<?php

namespace app\admin\controller\user;

use app\common\common\lib\CxjSdk;
use app\common\common\model\member\member;
use app\common\controller\Backend;

/**
 * 会员管理
 *
 * @icon fa fa-user
 */
class User extends Backend
{

    protected $relationSearch = true;


    /**
     * @var \app\admin\model\User
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new member();
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->count();
            $list = $this->model
                    ->where($where)
                    ->order($sort, $order)
                    ->limit($offset, $limit)
                    ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 修改积分
     */
    public function up_integral(){

        $ids = input( 'ids');
        $row = $this->model->find( $ids );
        if ($this->request->isPost()){

            $oldMember = new \app\common\common\model\oldModel\member\member();
            $oldMemberInfo = $oldMember->where( array('union_id' => $row['union_id']) )->field('member_id')->find();

            if( !$oldMemberInfo ){
                $this->error( '修改失败');
            }
            $cxjApi = new CxjSdk();
            $cxjApi->setAction( 'setMemberScore');
            $result = $cxjApi->getResult( ['type' => input( 'type') , 'score' => input( 'score') , 'member_id'=> $oldMemberInfo['member_id']] );
            if( $result  && $result['code'] == 1 ){
                $this->error( '修改成功');
            } else {
                $this->error( '修改失败');
            }
        } else {

            $this->assign( 'row' , $row);
            return $this->view->fetch();
        }


    }

}
